/**
  ******************************************************************************
  * @author     Chris
  * @since      2024/6/24 21:20
  *
  * @file       hd_24cxx.h
  * @brief      Header file of 24CXX hardware driver.
  *
  * @note       This file contains the 24CXX object declaration.
  *
  * @warning    None.
  ******************************************************************************
  * Change Logs:
  *   Date          Author       Notes
  *   2024/6/24     Chris        the first version
  *
  ******************************************************************************
  */


#ifndef JLC_SKY_24CXX_H
#define JLC_SKY_24CXX_H

#include <pd_i2c.h>
#include "stm32f407xx.h"

struct FT24CxxVtbl;

/** @defgroup Object properties
  * @{
  */
typedef struct {
    I2C *i2c;

    // AT24Cxx virtual table
    struct FT24CxxVtbl* vptr;
} FT24Cxx;
/**
 * @}
 */

/** @defgroup Object virtual table definition
  * @{
  */
struct FT24CxxVtbl {
    void (*init)(const FT24Cxx *this);

    void (*read)(const FT24Cxx *this, uint16_t address, uint8_t *dat, uint8_t len);

    void (*write)(const FT24Cxx *this, uint16_t address, uint8_t *dat, uint8_t len);
};
/**
 * @}
 */

/** @defgroup Object constructor and member function declaration
  * @{
  */
void FT24Cxx_constructor(FT24Cxx *this, I2C *i2c);
/**
 * @}
 */

/** @defgroup Object virtual table function implementation
  * @{
  */
static inline void FT24Cxx_init(const FT24Cxx *this) {
    this->vptr->init(this);
}

static inline void FT24Cxx_read(const FT24Cxx *this, uint16_t address, uint8_t *dat, uint8_t len) {
    this->vptr->read(this, address, dat, len);
}

static inline void FT24Cxx_write(const FT24Cxx *this, uint16_t address, uint8_t *dat, uint8_t len) {
    this->vptr->write(this, address, dat, len);
}
/**
 * @}
 */


#define FT24CXX_ADDRESS     0x54    // | 1 | 0 | 1 | 0 | A2 | A1 | A0 |
                                    //   1   0   1   0   x    x    x
#define FT24CXX_PAGE_SIZE     64    // The FT24C256A devices have 512 pages. Since each page has 64 bytes,
                                    // , random word addressing to FT24C256A will require 15 bits data word addresses

#endif //JLC_SKY_24CXX_H
